package org.stars.dao.sqlmapper.parser;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.stars.dao.sqlmapper.SqlBase;
import org.stars.dao.sqlmapper.SqlDefinition;
import org.stars.dao.sqlmapper.SqlMapper;

import static org.stars.config.Message.TEMPLATE_PARSER_01;
import static org.stars.config.Message.TEMPLATE_PARSER_02;

/**
 * Get the template string and analize the query body.
 * 
 * @author Francesco Benincasa
 *
 */
public abstract class TemplateParser {

	public static void parse(SqlMapper sqlMapper) throws Exception
	{
		Log log=LogFactory.getLog(TemplateParser.class);

		log.debug(TEMPLATE_PARSER_01.with(sqlMapper.getId()));
		
		for (SqlDefinition item: sqlMapper.getMap().values())
		{
			parse(item, item);
		}
		
		log.debug(TEMPLATE_PARSER_02.with(sqlMapper.getId()));
	}
	
	public static void parse(SqlDefinition sqlCurrent, SqlBase item)
	{
		SubParser01_CommentRemover.execute(item);
		SubParser02_WhiteSpaceRemover.execute(item);
		SubParser03_CheckBracket.execute(item);
		SubParser04_MacroReplacer.execute(sqlCurrent.getId(), item);				
	}
}
